<?php

include(dirname(__FILE__) . '/PHPWord_0.6.2_Beta/PHPWord.php');

class FileMannagament extends PHPWord {

	/** @var string File Name */
	private $file;

	/** @var string File Content/Data */
	private $content;

	/** @var string File Mime/Type */
	private $type;

	/** @var object File Stream */
	private $f_conn;

	/**
	 * @desc Object Constructor  
	 * @param string File Name 
	 * @param string File Content 
	 * @param string File Mime/Type 
	 */
	public function FileMannagament($f_name = "contract.docx", $f_content = false, $f_type = false) {
		$this->file = $f_name;
		$this->content = $f_content;
		$this->type = $f_type;

		parent::__construct();
	}

	/** @desc write the $content to the $file */
	private function write_file() {
		$this->f_conn = @fopen($this->file, 'w+');
		@fputs($this->f_conn, $this->content, strlen($this->content));
		@fclose($this->f_conn);
		return;
	}

	/** @desc prompt the user to download the $file */
	private function download_file() {
		header("Content-type: " . $this->type);
		header("Content-Disposition: attachment; filename=" . $this->file);
		header("Content-Length: " . filesize($this->file));
		@readfile($this->file);
	}

	private function dateFormat($date) {
		//201404230116
		$year = substr($date, 0, 4);
		$month = substr($date, 4, 2);
		$day = substr($date, 6, 2);

		return "$day/$month/$year";
	}

	public function generateContract($user, $car, $reservation, $conductor = false, $services = false) {

		
		if (!isset($_SESSION['enterprise']))
				$document = $this->loadTemplate('doc/contract.docx');
			else
				$document = $this->loadTemplate('doc/contract-enterprise.docx');				

		$document->setValue('reservation', $reservation['reservation_number']);
		$document->setValue('date', date('d/m/Y'));
		$document->setValue('date_actual', date('d/m/Y'));
		$document->setValue('dateM', date('d/m/Y'));

		if ($conductor) {

			$document->setValue('conductor', $conductor['sex'] . '. ' . utf8_decode($conductor['name']) . " " . utf8_decode($conductor['last_name']));
			$document->setValue('address', utf8_decode($conductor['address']));
			$document->setValue('movil', $conductor['phone_movil']);
			$document->setValue('postal_code', utf8_decode($conductor['postal_code']));
		} else {
			$document->setValue('conductor', $user['sex'] . '. ' . utf8_decode($user['name']) . " " . utf8_decode($user['last_name']));
			$document->setValue('address', utf8_decode($user['address']));
			$document->setValue('movil', $user['phone_movil']);
			$document->setValue('postal_code', utf8_decode($user['postal_code']));
		}
		$document->setValue('email', utf8_decode($user['email']));

		$document->setValue('date_fly', $this->dateFormat($reservation['date_fly']) . " " . $reservation['hour_fly']);
		$document->setValue('destination', utf8_decode($reservation['destination']));
		$document->setValue('company', utf8_decode($reservation['company_fly']));
		$document->setValue('fly_number', $reservation['fly_number']);
		$document->setValue('date_car_in', $this->dateFormat($reservation['day_date_car_in']) . ' ' . $reservation['hour_car_in']);
		$document->setValue('datein', $this->dateFormat($reservation['day_date_car_in']));
		$document->setValue('dateout', $this->dateFormat($reservation['day_date_car_out']));
		$document->setValue('date_fly_back', $this->dateFormat($reservation['date_fly_back']) . ' ' . $reservation['hour_fly_back']);
		$document->setValue('destination_back', utf8_decode($reservation['destination_fly_back']));
		$document->setValue('company_back', utf8_decode($reservation['company_fly_back']));
		$document->setValue('fly_number_back', $reservation['fly_number_back']);
		$document->setValue('date_car_out', $this->dateFormat($reservation['day_date_car_out']) . ' ' . $reservation['hour_car_out']);
		$document->setValue('baggage', $reservation['baggage']);


		if ($reservation['remarques'] || isset($_SESSION['enterprise_name'])) {

			if (isset($_SESSION['enterprise_name'])) {
				$document->setValue('remarques', "Remarques:");

				if ($reservation['remarques'])
					$document->setValue('remarquesV', $_SESSION['enterprise_name'] . ": " . utf8_decode($reservation['remarques']));
				else
					$document->setValue('remarquesV', $_SESSION['enterprise_name']);
			}
			else if ($reservation['remarques'])
				$document->setValue('remarquesV', utf8_decode($reservation['remarques']));;
		} else {
			$document->setValue('remarques', '');
			$document->setValue('remarquesV', '');
		}

		$cont = 1;
		$chargeService = 0;

		if ($services && is_array($services)) {

			foreach ($services as $value) {

				$document->setValue('s' . $cont, 1);

				$document->setValue('chf' . $cont, 'CHF');

				$document->setValue('service' . $cont, utf8_decode($value['name']));

				$document->setValue('servicec' . $cont++, $value['charge'] . ',00');

				$chargeService += $value['charge'];
			}
		}

		for ($i = 1; $i < 3; $i++) {

			$document->setValue('chf' . $i, '');

			$document->setValue('s' . $i, ' ');

			$document->setValue('service' . $i, ' ');

			$document->setValue('servicec' . $i, ' ');
		}

		$document->setValue('gar', $reservation['days_car_in']);
		$document->setValue('charge', ($reservation['charge'] - $chargeService - 25) . ',00');
		$document->setValue('total', $reservation['charge'] . ',00');

		$contractPath = 'doc/contracts/user' . $user['userid'] . 'date' . $reservation['date'];

		if (!isset($_SESSION['enterprise'])) {
			$totalp = floatval($reservation['charge']) - ( floatval($reservation['charge']) * 0.05 );
			$totalp = number_format($totalp, 2, ',', ' ');
			$document->setValue('totalp', $totalp);
		}

		$document->setValue('dateF', date('d/m/Y'));

		$document->setValue('mark', utf8_decode($car['mark']));
		$document->setValue('plate', utf8_decode($car['plate']));
		$document->setValue('model', utf8_decode($car['model']));
		$document->setValue('color', utf8_decode($car['color']));


		if (mkdir($contractPath)) {

			chmod($contractPath, 0777);
		
				$contractPath .= '/contract.docx';
				
			
			if ($document->save($contractPath)) {

				return $contractPath;
			}
		}


		return false;
	}

}

?>
